<?php
/**
 * 余额支付类
 * @author chenlisong E-mail:chenlisong1021@163.com 
 * @version 创建时间：2016年11月14日 上午10:42:32 
 * 类说明
 */
class PayAction extends BaseAction{
	
	
	/**
	 * 余额支付
	 */
	public function balancePayOrder(){
		$Order = M('Order');$User = M('User');$UserDetails = M("UserDetails");
		$param = $this->param;
		$userId = $_SESSION['HS_USER_ID'];
		$Order->startTrans();
		if(empty($param['orderCode'])){
			$this->ajaxReturn(0,"参数错误！",0);
		}
		//判断订单状态是否正确
		$where['code'] = $param['orderCode'];
		$where['flag'] = 1;
		$where['status'] = 1;
		$info = $Order->where($where)->find();
		if(count($info) == 0){
			$this->ajaxReturn(0,"错误的订单状态，请重新下单！",0);
		}
		//判断余额是否充足
		$balance = (float) $User->where('flag = 1 and id = '.$userId)->getField('balance');
		$price = $info['real_price'] + $info['mail_fee'];
		if($price > $balance){
			$this->ajaxReturn(0,"余额不足，请先充值！",0);
		}
		//记录明细
		$detailData['user_id'] = $userId;
		$detailData['title'] = "订单支付";
		$detailData['type'] = 2;
		$detailData['price'] = $price;
		$detailData['last_price'] = $balance;
		$detailData['create_time'] = time();
		$result1 = $UserDetails->add($detailData);
		$result2 = $User->where('flag = 1 and id = '.$userId)->setDec('balance',$price);
		//订单状态更改
		$data['status'] = 2;
		$data['flag'] = 1;
		$data['pay_type'] = 3;
		$data['pay_time'] = time();
		$data['update_time'] = time();
		$result3 = $Order->where($where)->save($data);
		if($result1 && $result2 && $result3 !== false){
			$Order->commit();
			$this->ajaxReturn(1,'支付成功！',1);
		}else{
			$Order->rollback();
			$this->ajaxReturn(0,"支付失败！",0);
		}
	}
}